﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using FrankJie.IDAL;
using FrankJie.Model;

namespace FrankJie.SQLServerDAL
{
	/// <summary>
	/// 评论数据访问类
	/// </summary>
	public partial class Comment:IComment
	{
        /// <summary>
        /// 增加一条评论
        /// </summary>
        /// <param name="commentInfo">评论实体</param>
        /// <returns>返回评论ID</returns>
        public int Add(CommentInfo commentInfo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("INSERT INTO tblComment(");
			strSql.Append("PostId,Author,Contents,Email,Url,Ip,CreateTime,Checked,Agent,ParentId)");
			strSql.Append(" VALUES (");
			strSql.Append("@PostId,@Author,@Contents,@Email,@Url,@Ip,@CreateTime,@Checked,@Agent,@ParentId)");
			strSql.Append(";SELECT @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@PostId", SqlDbType.Int,4),
					new SqlParameter("@Author", SqlDbType.VarChar,50),
					new SqlParameter("@Contents", SqlDbType.VarChar,1000),
					new SqlParameter("@Email", SqlDbType.VarChar,50),
					new SqlParameter("@Url", SqlDbType.VarChar,200),
					new SqlParameter("@Ip", SqlDbType.VarChar,20),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Checked", SqlDbType.Bit,1),
					new SqlParameter("@Agent", SqlDbType.VarChar,200),
					new SqlParameter("@ParentId", SqlDbType.Int,4)};
            parameters[0].Value = commentInfo.PostId;
            parameters[1].Value = commentInfo.Author;
            parameters[2].Value = commentInfo.Contents;
            parameters[3].Value = commentInfo.Email;
            parameters[4].Value = commentInfo.Url;
            parameters[5].Value = commentInfo.Ip;
            parameters[6].Value = commentInfo.CreateTime;
            parameters[7].Value = commentInfo.Checked;
            parameters[8].Value = commentInfo.Agent;
            parameters[9].Value = commentInfo.ParentId;

			object obj = SQLHelper.GetSingle(SQLHelper.FrankJieDBConnStr,CommandType.Text,strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		
		/// <summary>
		/// 删除评论通过ID
		/// </summary>
        /// <param name="id">评论ID</param>
		/// <returns></returns>
		public bool Delete(int id)
		{
            string strSql = "DELETE FROM tblComment WHERE Id = @Id";
            SqlParameter[] parameters = {
                new SqlParameter("@PostId", SqlDbType.Int,4)};
            parameters[0].Value = id;
			int rows=SQLHelper.ExecuteSql(SQLHelper.FrankJieDBConnStr,CommandType.Text,strSql);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        /// <summary>
        /// 检验通过评论通过ID集合 例1,2,3,4
        /// </summary>
        /// <param name="idList">评论ID集合</param>
        /// <returns>返回是否成功</returns>
        public bool CheckPass(string idList)
        {
            string strSql = "UPDATE tblComment SET Checked = 'true' WHERE Id in (" + idList + ")";
            int rows = SQLHelper.ExecuteSql(SQLHelper.FrankJieDBConnStr, CommandType.Text, strSql);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
	}
}

